//
//  FindViewController.m
//  Travel assistant
//
//  Created by 王京 on 16/3/2.
//  Copyright © 2016年 旅游助手. All rights reserved.
//

#import "FindController.h"
#import "FindCollectionViewCell.h"
#import "Find.h"
#import "DataHelper.h"
#import <UIImageView+WebCache.h>
#import "FindWebViewController.h"
@interface FindController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

@property (nonatomic,strong) UICollectionView *collectionView;

@property (nonatomic,strong) UICollectionViewFlowLayout *layout;


@end

NSInteger number1 = 1;

@implementation FindController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self addCollectionView];
    //隐藏返回按钮
    self.navigationItem.hidesBackButton = YES;
    //滑动条隐藏
    self.collectionView.showsHorizontalScrollIndicator = NO;
    //单页翻转
    self.collectionView.pagingEnabled = YES;
    //
    self.collectionView.backgroundColor = [UIColor whiteColor];
    //用户交互
    self.collectionView.userInteractionEnabled = YES;
    
    self.navigationItem.title = @"发现";
    //
    UIBarButtonItem *rightBarButton = [[UIBarButtonItem alloc]initWithTitle:@"列表" style:(UIBarButtonItemStylePlain) target:self action:@selector(RightbarbuttonAction)];
    
    self.navigationItem.rightBarButtonItem = rightBarButton;
    
    //上划手势
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeAction)];
    
    [swipe setDirection:(UISwipeGestureRecognizerDirectionUp)];
    
    [self.view addGestureRecognizer:swipe];
    
    
    // Do any additional setup after loading the view.
}

- (void)addCollectionView{
    
    self.layout = [[UICollectionViewFlowLayout alloc]init];
    
    self.layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    
    self.layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    self.layout.itemSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
    
    self.layout.minimumLineSpacing = 0;
    
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:self.layout];
    
    self.collectionView.delegate = self;
    
    self.collectionView.dataSource = self;
    
    [self.view addSubview:self.collectionView];
    
    [self.collectionView registerClass:[FindCollectionViewCell class] forCellWithReuseIdentifier:@"Cell"];
    
    
    
    
}
//懒加载
-(NSMutableArray *)dataArray{
    
    if (_dataArray == nil) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

//右按钮的方法
- (void)RightbarbuttonAction{
    
    [self.navigationController popToRootViewControllerAnimated:YES];
    
    self.tabBarController.tabBar.hidden = NO;
    
    HELP.flag = YES;
    
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    return 1;
    
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.dataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    FindCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
    
    Find *fd = self.dataArray[indexPath.row];
    
    cell.titleLabel.text = fd.title;
    
    cell.detailLabel.text = fd.mentor;
    //传回下标
    HELP.index = indexPath;
    
    [cell.imageV sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://travelbox-interface.xyz.cn%@",fd.url]]];
    
    
    return cell;
}


- (void)viewWillAppear:(BOOL)animated{
    //点击tabCell跳转到特定collectionCell
    [self.collectionView selectItemAtIndexPath:self.index animated:YES scrollPosition:(UICollectionViewScrollPositionCenteredHorizontally)];
    
    [self.collectionView reloadData];
}

//滑动手势
-(void)swipeAction{
    
    [self.navigationController popToRootViewControllerAnimated:YES];
    
    self.tabBarController.tabBar.hidden = NO;
    
    HELP.flag = YES;
}

//选择collectionCell
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    FindWebViewController *f = [[FindWebViewController alloc]init];
    
    f.find = self.dataArray[indexPath.row];
    
    [self.navigationController pushViewController:f animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
